﻿using Ms = Jp.NicoVideo.Live.Api.PlayerStatusDataSet.msRow;
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Xml;

namespace Jp.NicoVideo.Live.Api
{
    public class PlayerStatus
    {
        /// <summary>
        /// メッセージサーバーに接続するための情報を取得する
        /// </summary>
        /// <param name="liveID">対象の放送ID</param>
        /// <param name="cookies">セッション情報が保存されたクッキーコンテナ</param>
        /// <returns>接続用の情報</returns>
        public static PlayerStatusDataSet Get(string liveID)
        {
            string url = String.Format("http://live.nicovideo.jp/api/getplayerstatus?v={0}", liveID);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.CookieContainer = CookieMonster.Vomit();
            PlayerStatusDataSet playerStatus = new PlayerStatusDataSet();

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            using (StreamReader stream = new StreamReader(response.GetResponseStream()))
            using (XmlReader reader = XmlReader.Create(stream))
            {
                playerStatus.ReadXml(reader);
                Console.WriteLine(playerStatus.stream.Count);

                if (playerStatus.getplayerstatus.Count == 0 ||
                    playerStatus.getplayerstatus[0].status != "ok")
                {
                    playerStatus.Dispose();
                    playerStatus = null;
                }
            }

            return playerStatus;
        }
    }
}
